package com.ygqh.baby.controller.mobile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.CommentType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.model.YgMessageModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgMessageService;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.SessionUtil;

/**
 * 消息相关接口
 * 
 * @author guohao
 *
 */
@Controller
@RequestMapping("/mobile/message")
public class YgMessageController  {

	@Autowired
	private YgMessageService messageService;
	@Autowired
	private DocumentConstant documentConstant;
	/**
	 * 获取消息列表
	 * 
	 * @param queryInfo
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findUserMessageList")
	@ResponseBody
	public JSONPObject findUserMessageList(QueryInfo queryInfo, String callback, @RequestParam(required=true)CommentType commentType) {
		JSONPObject json = null;
		YgUser user = SessionUtil.getCurrentUser();
		List<YgMessageModel> list = messageService.findUserMessageList(queryInfo, user.getId(), null,commentType);
		for (YgMessageModel model : list) {

			String format = DateConvertUtils.formatDateTime(model.getCreateTime());
			String[] split = format.split(" ");
			String[] date = split[0].split("-");
			String[] time = split[1].split(":");
			if (DateConvertUtils.isToday(model.getCreateTime())) {
				model.setCreateTimeString(time[0] + ":" + time[1]);
			} else {
				model.setCreateTimeString(date[1] + "月" + date[2] + "日");
			}
		}
		json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
		return json;
	}

	/**
	 * 获取未读消息数量
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findUnReadCount")
	@ResponseBody
	public JSONPObject findUnReadCount(String callback) {
		JSONPObject json = null;
		YgUser user = SessionUtil.getCurrentUser();
		int count = messageService.findUnReadCount(user.getId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count", count);
		json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, map));
		return json;
	}

	/**
	 * 删除
	 * 
	 * @param messageId
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "deleteMessage")
	@ResponseBody
	public JSONPObject deleteMessage(Long messageId, String callback) {
		JSONPObject json = null;
		YgUser user = SessionUtil.getCurrentUser();
		int row = messageService.deleteSoft(messageId, user.getId());
		json = new JSONPObject(callback, Message.success("操作成功", row));
		return json;
	}

	@RequestMapping(value = "readMessage")
	@ResponseBody
	public JSONPObject readMessage(Long messageId, String callback) {
		JSONPObject json = null;
		YgUser user = SessionUtil.getCurrentUser();
		int row = messageService.readMessage(messageId, user.getId());
		json = new JSONPObject(callback, Message.success("操作成功", row));
		return json;
	}
}
